/*
 * @Author: szx
 * @Date: 2021-10-29 13:29:52
 * @LastEditTime: 2021-10-29 13:33:14
 * @Description:
 * @FilePath: \leetcode\349\349.cpp
 */

#include <algorithm>
#include <iostream>
#include <unordered_set>
#include <vector>
using namespace std;
class Solution {
   public:
    vector<int> intersection(vector<int>& nums1, vector<int>& nums2) {
        if (nums1.size() > nums2.size()) {
            return intersection(nums2, nums1);
        }
        unordered_set<int> m;
        for (int num : nums1) {
            m.insert(num);
        }
        vector<int> v;
        for (int num : nums2) {
            auto it = m.find(num);
            if (it != m.end()) {
                v.push_back(num);
                m.erase(num);
            }
        }
        return v;
    }
};